﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using GrewalCRM.Entities;

namespace GrewalCRM.DAL
{
  



  public class SMSFailure_DAL : BaseDALGrewalCRM, IBaseDALGrewalCRM
  {
      #region Protected Methods
      public override int AddNew(BaseEntitiesGrewalCRM Entity)
      {
          int result = 0;
          SMSFailure failedSMS = Entity as SMSFailure;
          Cmd.CommandText = "usp_InsertFailedSMS";
          SetParametersForDBSave(failedSMS);
          Cmd.Connection.Open();
          Cmd.ExecuteNonQuery();
          result = (int)(Cmd.Parameters["@result"].Value);
          Cmd.Connection.Close();
          return result;
      }

      public override int Update(BaseEntitiesGrewalCRM Entity)
      {
          throw new NotImplementedException();
      }

      public override int Delete(BaseEntitiesGrewalCRM Entity)
      {
          int result = 0;
          SMSFailure failedSMS = Entity as SMSFailure;
          Cmd.CommandText = "usp_DeleteFailedSMS";
          SetParametersForDBSave(failedSMS);
          Cmd.Parameters.AddWithValue("@SMSFailureID", Entity.ID);
          Cmd.Connection.Open();
          Cmd.ExecuteNonQuery();
          result = (int)(Cmd.Parameters["@result"].Value);
          Cmd.Connection.Close();
          return result;
      }
      #endregion

      #region Public Method
      public int Save(BaseEntitiesGrewalCRM Entity)
      {
          throw new NotImplementedException();
      }
      public override List<BaseEntitiesGrewalCRM> GetAll()
      {
          Cmd.CommandText = "usp_GetAllFailedSMS";
          Cmd.Connection.Open();
          Rdr = Cmd.ExecuteReader();
          List<BaseEntitiesGrewalCRM> allFailedSMS = new List<BaseEntitiesGrewalCRM>();

          if (Rdr != null && Rdr.HasRows)
          {
              while (Rdr.Read())
              {
                  SMSFailure failedSMS = new SMSFailure(Rdr[0] != null && Rdr[0].GetType() != typeof(DBNull) ? Rdr.GetInt32(0) : 0,
                                                        Rdr[1] != null && Rdr[1].GetType() != typeof(DBNull) ? Rdr.GetInt32(1) : 0,
                                                        Rdr[2] != null && Rdr[2].GetType() != typeof(DBNull) ? Rdr.GetDateTime(2) : DateTime.MinValue,
                                                        Rdr[3] != null && Rdr[3].GetType() != typeof(DBNull) ? Rdr.GetString(3) : string.Empty,
                                                        Rdr[4] != null && Rdr[4].GetType() != typeof(DBNull) ? Rdr.GetString(4) : string.Empty,
                                                        Rdr[5] != null && Rdr[5].GetType() != typeof(DBNull) ? Rdr.GetString(5) : string.Empty,
                                                        Rdr[6] != null && Rdr[6].GetType() != typeof(DBNull) ? Rdr.GetString(6) : string.Empty,
                                                        Rdr[7] != null && Rdr[7].GetType() != typeof(DBNull) ? Rdr.GetString(7) : string.Empty
                                                       );
                  allFailedSMS.Add(failedSMS);
              }
          }
          Cmd.Connection.Close();

          return allFailedSMS;
      }
      public override BaseEntitiesGrewalCRM GetByID(int ID)
      {
          throw new NotImplementedException();
      }
      public override void FillEntity(BaseEntitiesGrewalCRM Entity)
      {
          throw new NotImplementedException();
      }
      #endregion

      #region Private Method
      private void SetParametersForDBSave(BaseEntitiesGrewalCRM Entity)
      {
          SMSFailure failedSMS = Entity as SMSFailure;
          Cmd.Parameters.Clear();

          Cmd.Parameters.Add("@result", System.Data.SqlDbType.Int).Direction = System.Data.ParameterDirection.Output;

          //if (failedSMS.CallID != null && failedSMS.CallID.Trim() != string.Empty)
          Cmd.Parameters.AddWithValue("@CallID", failedSMS.CallID);

          //if (failedSMS.SMSSendDate != null && failedSMS.SMSSendDate.Trim() != string.Empty)
          Cmd.Parameters.AddWithValue("@SMSSendDate", failedSMS.SMSSendDate);

          if (failedSMS.RecipientType != null && failedSMS.RecipientType.Trim() != string.Empty)
              Cmd.Parameters.AddWithValue("@RecipientType", failedSMS.RecipientType);

          if (failedSMS.RecipientName != null && failedSMS.RecipientName.Trim() != string.Empty)
              Cmd.Parameters.AddWithValue("@RecipientName", failedSMS.RecipientName);

          if (failedSMS.RecipientMobile != null && failedSMS.RecipientMobile.Trim() != string.Empty)
              Cmd.Parameters.AddWithValue("@RecipientMobile", failedSMS.RecipientMobile);

          if (failedSMS.RecipientSMS != null && failedSMS.RecipientName.Trim() != string.Empty)
              Cmd.Parameters.AddWithValue("@RecipientSMS", failedSMS.RecipientSMS);

          if (failedSMS.ResponseCode != null && failedSMS.ResponseCode.Trim() != string.Empty)
              Cmd.Parameters.AddWithValue("@ResponseCode", failedSMS.ResponseCode);


      }
      #endregion
  }
}
